package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Room;
import com.qf.entity.RoomDTO;
import com.qf.entity.RoomVO;
import com.qf.listener.RoomDataListener;
import com.qf.service.RoomService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @ClassName: RoomController
 * @Author: WangHao
 * @Date: 2025/4/27 15:46
 * @Description:
 * @Version: 1.0
 */
@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    // 分页查询
    @PostMapping("/findAll")
    public R findAll(@RequestBody RoomDTO roomDTO) {
        PageHelper.startPage(roomDTO.getPageNum(), roomDTO.getPageSize());

        List<Room> list =roomService.findAll(roomDTO);
        PageInfo<Room> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    // 根据id单个删除房间
    @GetMapping("/delRoomById")
    @PreAuthorize("hasAuthority('system:user:delete')")
    public R delRoomById(Integer id) {
        boolean op =  roomService.delRoomById(id);
        if (!op){
            return R.fail();
        }
        return R.ok();
    }

    // 批量删除房间
    // TODO:退出后token没有清空，前端携带的token还是上次登录的用户
    @GetMapping("/delRoomByIds")
    @PreAuthorize("hasAuthority('system:user:delete')")
    public R delRoomByIds(Integer[] ids) {
        boolean op =  roomService.delRoomByIds(ids);
        if (!op){
            return R.fail();
        }
        return R.ok();
    }

    // 添加房间
    @PostMapping("/addRoom")
    public R addRoom(@RequestBody RoomDTO roomDTO) {
        boolean op = roomService.addRoom(roomDTO);
        if (!op){
            return R.fail();
        }
        return R.ok();
    }

    // 更新房间信息
    @PostMapping("/updateRoom")
    public R updateRoom(@RequestBody RoomDTO roomDTO) {
        boolean op = roomService.updateRoom(roomDTO);
        if (!op){
            return R.fail();
        }
        return R.ok();
    }

    // 获取房间类型
    @GetMapping("/findRoomType")
    public R getRoomType() {
        List<RoomVO> list = roomService.getRoomType();
        return R.ok(list);
    }

    // 批量导入
    @PostMapping("/uploadExcel")
    public R uploadExcel(MultipartFile file) {
        try {
            // 重点,此处第三个参数需要传入Service对象
            EasyExcel.read(file.getInputStream( ),
                            Room.class,
                            new RoomDataListener(roomService)).sheet( ).doRead( );
        }catch (Exception e){
            e.printStackTrace();
            return R.fail();
        }
        return R.ok( );
    }

    /**
     * 下载Excel文件
     * 注意,返回值是void
     */
    @GetMapping("/download/excel")
    public void downloadExcel(HttpServletResponse response) throws IOException {

        //  查询数据库全部数据
        List<Room> list = roomService.findAll(null);

        // 导出.xls用这个
        // response.setContentType("application/vnd.ms-excel");
        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("房间信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        // easyexcel导出工具
        EasyExcel.write(response.getOutputStream(), Room.class).autoCloseStream(Boolean.FALSE).sheet("房间信息")
                .doWrite(list);
    }


}
